/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leadiv.openrule;

import java.awt.Color;

/**
 *
 * @author leadiv
 */
public class Utilities {
    // hexadecimal string to color format: #HHHHHH | HHHHHH | HHH | #HHH
    public static Color hstoc(String c){
        int r = 0, g = 0, b = 0;

        if(c.startsWith("#"))
            c = c.substring(1);

        if(c.length() == 3) {
            r = Integer.parseInt(c.substring(0, 1).concat(c.substring(0, 1)), 16);
            g = Integer.parseInt(c.substring(1, 2).concat(c.substring(1, 2)), 16);
            b = Integer.parseInt(c.substring(2, 3).concat(c.substring(2, 3)), 16);
        }

        if(c.length() == 6) {
            r = Integer.parseInt(c.substring(0, 2), 16);
            g = Integer.parseInt(c.substring(2, 4), 16);
            b = Integer.parseInt(c.substring(4, 6), 16);
        }

        return new Color(r, g, b);
    }

    // color object to hexadecimal string
    public static String ctohs(Color c) {
        return String.format(
                "%1$02x%2$02x%3$02x",
                c.getRed(),
                c.getGreen(),
                c.getBlue());
    }
}
